package handlers

import (
	"github.com/gin-gonic/gin"
	"scenic-vid-mgr/models"
	"scenic-vid-mgr/pkg"
	"strconv"
)

func Login(c *gin.Context) {
	name := c.Query("name")
	pass := c.Query("pass")
	var login models.User
	err := models.DB.Where("name = ?", name).First(&login).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户名不存在",
		})
		return
	}
	if pkg.Md(pass) != login.Pass {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "登录成功",
		})
		return
	}
}
func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size

	var count int64
	models.DB.Model(&models.Life{}).Count(&count)

	var list []models.Life
	err := models.DB.Offset(offset).Limit(size).Find(&list).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "查询成功",
			"data": gin.H{
				"count": count,
				"list":  list,
			},
		})
		return
	}
}
